/**
 * 通用工具类
 */

/*
 * 模拟form表单提交，单个参数提交
 * action : form action url
 * paramName : 参数名称
 * paramValue ： 参数值
 * method ： POST/GET
 */
function formSubmitOfSingleParam(action,paramName,paramValue,method){
	document.write('<form name=myForm></form>');
	var myForm=document.forms['myForm'];
	myForm.action=action;
	myForm.method=method;
	 
	var input = document.createElement('input');
	input.type = 'hidden';
	input.name = paramName;
	input.value = paramValue;
	myForm.appendChild(input);
	myForm.submit();
}

/*
 * 模拟form表单提交，多个参数提交
 * action : form action url
 * paramNameS : 参数名称 数组
 * paramValueS ： 参数值 数组
 * method ： POST/GET
 */
function formSubmitOfMultiParam(action,paramNameS,paramValueS,method){
	document.write('<form name=myForm></form>');
	var myForm=document.forms['myForm'];
	myForm.action=action;
	myForm.method=method;
	 
	for(var i=0;i<paramNameS.length;i++){
		var input = document.createElement('input');
		input.type = 'hidden';		//可进一步封装
		var paramName = paramNameS[i];
		var paramValue = paramValueS[i];
		input.name = paramName;
		input.value = paramValue;
		myForm.appendChild(input);
	}
	myForm.submit();
}

/*
 *  个性化显示时间，规则：
 *  今天显示："今天HH:mm", 
 *  昨天显示： "昨天HH:mm", 
 * 一个星期以内的显示： "星期X HH:mm"
 * 一个星期以前显示：" yyyy-MM-dd HH:mm"
 * @param time  格式 "yyyy-MM-dd HH:mm"
 */
function getSpecialTimeToShow(time){
	if (time == null || time == "") {
        return "";
    }
	var specialTime = time;
	var aDate = time.substring(0,time.indexOf(' ')).split('-'); 
	
	var myDate = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
	var newDate = (new Date().getMonth() + 1) + "-" + new Date().getDate() + '-' + new Date().getFullYear();
	var nowDate = new Date(Date.parse(newDate));
	if(myDate.getFullYear() != nowDate.getFullYear()){
		return specialTime;
	}
	   
	var dayDiffer = parseInt(Math.abs(nowDate - myDate) / 1000 / 60 / 60 /24); 
	if (dayDiffer < 0) { //未来的时间
        return specialTime;
    }  else if (dayDiffer == 0) { //今天时间
            specialTime = "今天" + time.substring(time.indexOf(' '));
    } else if (dayDiffer == 1) { //昨天的时间
            specialTime = "昨天" + time.substring(time.indexOf(' '));
    } else if (dayDiffer < 7){ //一个星期以内的时间
    	specialTime = getWeekDayOfDate(time) + time.substring(time.indexOf(' '));
    } 
	return specialTime;
}

function getWeekDayOfDate(time){
	var aDate = time.substring(0,time.indexOf(' ')).split('-'); 
	var myDate = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
	if(myDate.getDay() == 0){
		return "星期日";
	} else if(myDate.getDay() == 1){
		return "星期一";
	} else if(myDate.getDay() == 2){
		return "星期二";
	} else if(myDate.getDay() == 3){
		return "星期三";
	} else if(myDate.getDay() == 4){
		return "星期四";
	} else if(myDate.getDay() == 5){
		return "星期五";
	} else{
		return "星期六";
	}
}

/**
 * 加密
 * 
 * @param content
 * @param srcKey
 * @returns
 */
function encrypt(content,srcKey){
	return hex_md5(content).toString();
	
    /*var keyLen = 8;
	  var srcKeyLen = srcKey.length;
	
	 if(srcKeyLen > keyLen){
		 srcKey = srcKey.substr(0,keyLen);
		
	 }else{
		 var paddLen = keyLen -  srcKeyLen;
		 for(var i=0;i<paddLen;i++){
			 srcKey += "0";
		 }
	 }
	
	 var key = CryptoJS.enc.Utf8.parse(srcKey);		
	 var encryptStr =  CryptoJS.DES.encrypt(content, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
	return encryptStr.toString();*/
}

var chars = ['0','1','2','3','4','5','6','7','8','9'];

/**
 * 获取随机密码，6位数
 * 
 * @returns {String}
 */
function randomPwd() {
     var res = "";
     for(var i = 0; i < 6 ; i ++) {
         var id = Math.ceil(Math.random()*9);
         res += chars[id];
     }
     return res;
}

Array.prototype.indexOf = function(val) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == val)
			return i;
	}
	return -1;
};

Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
		this.splice(index, 1);
	}
};


function checInputNum(obj) {
	obj.value = obj.value.replace(/[^\d]/g, '');
}
